$(document).ready(function () {
    $('#example').DataTable({
        // fixedHeader: true
    });
});

function search() {
    let optionJson = [];
    let key_word = $("input[name='key_word']").val();
    if (key_word == 0) {
        key_word = "骨松"
    }
    let request_parameters = {type: "get_group_info", group_keyword: key_word};
    $.ajax({
        type: "post",
        url: "http://127.0.0.1:9998/analysis/get_group_info",
        data: request_parameters,
        async: true,
        datatype: "Json",
        success: function (group_info) {
            optionJson = group_info;
            console.log(optionJson.data);
            $("#example").dataTable().fnDestroy();//还原初始化
            $('#example').DataTable({
                // "serverSide":true,
                //  "paging":true,
                // "lengthMenu":[15,25,35,100,25],
                // "pagingType":"scrolling",
                // serverSide:true,
                // Processing:true,
                // deferRender:true,
                bStateSave: true,//翻页时保存状态
                // iDisplayLength:10,
                // iDisplayStart:0,
                // ordering:false,//禁用全局排序
                data: optionJson.data,
                //设置列数据
                columns: [
                    {
                        "data": 'groupId',
                        "width": '7%',
                        "render": function (data, type, row, meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }

                    },
                    {
                        "data": 'groupType',
                        "width": '10%',
                        "render": function (data, type, row, meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    },
                    {
                        "data": 'groupLeader',
                        "width": '13%',
                        "render": function (data, type, row, meta) {
                            return data = '<p style="text-align: center">' + data + '</p>'
                        }
                    },
                    {"data": 'keywordLocal',
                        "width":'60%',
                        "render":function (data,type,row,meta) {
                            let str,str1,str2 = "";
                            // console.log(row.group_keyword);
                            //循环遍历group_keyword数组
                            groupKeywordList = row.groupKeyword.split(",")
                            for(let i in groupKeywordList){
                                //若改行下标在key_word数组中，将其字体颜色置为红色
                                for(let j in data) {
                                    if(i == data[j].valueOf()){
                                        if(i == groupKeywordList.length-1){//判断是否是最后一个关键词
                                            str1 ='<span style="color: red">'+groupKeywordList[i]+'</span>';
                                            break;
                                        }
                                        else{
                                            str1 ='<span style="color: red">'+groupKeywordList[i]+','+'</span>';
                                            break;
                                        }
                                    }
                                    else {
                                        str1 ='<span style="color: black">'+groupKeywordList[i]+','+'</span>';
                                    }
                                }
                                str2 +=str1;
                            }
                            str = '<p>'+str2+'</p>';
                            return data = '<p style="text-align: center;">'+str+'</p>';
                        }
                    },
                    {
                        "data": 'groupLeader',
                        "width": '40%',
                        "text-align": 'center',
                        //方法
                        "render": function (data, type, row, meta) {
                            return data = '<button style="margin:0 auto;" class="btn btn-success" id="switch_key_ueser" onclick="Go_key_user(' + data + ')">查看关键人</button>'
                        }
                    }
                ],
                //设置排序
                "order": [[1, 'asc']],
                //设置语言
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项搜索结果",
                    "sZeroRecords": "没有匹配结果",
                    "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 条数据",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                    "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "未搜索到数据",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    },
                    "oAria": {
                        "sSortAscending": ": 以升序排列此列",
                        "sSortDescending": ": 以降序排列此列"
                    }
                },
            });
        }
    });
}

//前端将群体关键人号码传给后台，后台返回关键人信息
function Go_key_user(phone_num) {
    let clickPhone = phone_num;
    // window.localStorage.setItem("clickPhone", clickPhone);//设置指定key的数据（JSON格式）
    //将用户类型type_user设置为0，在gr.js判断type_user的值
    // document.getElementById("type_user_l").innerHTML = '0';
    //window.location.href = '/key_user';//跳转
    $.cookie("node_phone_num", clickPhone)
    creatIframe('/key_user', '关键人信息');
}


